package com.earthea.scaffold.system.controller;

import com.earthea.scaffold.common.aop.annotation.ControllerLog;
import com.earthea.scaffold.common.entity.BaseDataRsp;
import com.earthea.scaffold.common.entity.BaseRsp;
import com.earthea.scaffold.common.entity.PageResult;
import com.earthea.scaffold.common.enums.CommonStatusEnum;
import com.earthea.scaffold.common.enums.OperateTypeEnum;
import com.earthea.scaffold.common.util.ExcelUtil;
import com.earthea.scaffold.domain.SysFile;
import com.earthea.scaffold.system.entity.request.SysFileReq;
import com.earthea.scaffold.system.service.SysFileService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 文件Controller
 *
 * @author zhysunny
 * @date 2025-09-22
 */
@RestController
@RequestMapping("/api/system/file")
public class SysFileController {
    @Autowired
    private SysFileService sysFileService;

    /**
     * 查询文件列表
     */
    @PreAuthorize("@ss.hasPermission('system:file:list')")
    @GetMapping()
    public PageResult<SysFile> list(SysFileReq sysFile) {
        return sysFileService.selectPage(sysFile);
    }

    /**
     * 新增文件
     */
    @PreAuthorize("@ss.hasPermission('system:file:add')")
    @ControllerLog(title = "文件数据新增", operateType = OperateTypeEnum.INSERT)
    @PostMapping
    public BaseRsp add(@RequestParam("file") MultipartFile file) {
        return sysFileService.upload(file, CommonStatusEnum.DISABLED);
    }

    /**
     * 删除文件
     */
    @PreAuthorize("@ss.hasPermission('system:file:remove')")
    @ControllerLog(title = "文件数据删除", operateType = OperateTypeEnum.DELETE)
    @DeleteMapping("/{fileIds}")
    public BaseRsp remove(@PathVariable("fileIds") List<Long> fileIds) {
        return sysFileService.deleteByIdsAndStatus(fileIds);
    }

    /**
     * 下载文件
     *
     * @param response 响应
     * @param fileId   文件id
     */
    @PreAuthorize("@ss.hasPermission('system:file:query')")
    @ControllerLog(title = "文件数据下载", operateType = OperateTypeEnum.EXPORT)
    @GetMapping("/{fileId}")
    public void download(HttpServletResponse response, @PathVariable("fileId") Long fileId) {
        sysFileService.download(response, fileId);
    }
}
